package com.example.service;

import com.example.entity.PetCheckUpInfo;
import com.example.entity.PetHealth;
import com.example.entity.PetVaccine;
import com.example.mapper.PetHealthMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author HY
 * @date 2025/2/18
 */
@Service
public class PetHealthService {
    @Autowired
    private PetHealthMapper petHealthMapper;
    @Autowired
    private PetCheckUpInfoService petCheckUpInfoService;
    @Autowired
    private PetVaccineService petVaccineService;

    public void save(PetHealth petHealth) {
        Integer userId = TokenUtils.getCurrentUser().getId();
        petHealth.setUserId(userId);
        Integer count = petHealthMapper.countByUserId(userId);
        if (count > 0) {
            petHealthMapper.edit(petHealth);
        }else {
            petHealthMapper.insert(petHealth);
        }
    }

    public PetHealth selectByUserId() {
        Integer userId = TokenUtils.getCurrentUser().getId();
        return petHealthMapper.selectByUserId(userId);
    }

    public PageInfo<PetHealth> listPage(Integer pageSize, Integer pageNum, PetHealth petHealth) {
        PageHelper.startPage(pageNum, pageSize);
        return PageInfo.of(petHealthMapper.selectList(petHealth));
    }

    public PetHealth queryById(Integer id) {
        PetHealth petHealth = petHealthMapper.selectById(id);
        List<PetCheckUpInfo> petCheckUpInfoList = petCheckUpInfoService.list(petHealth.getUserId());
        List<PetVaccine> petVaccineList = petVaccineService.list(petHealth.getUserId());
        petHealth.setPetVaccineList(petVaccineList);
        petHealth.setPetCheckUpInfoList(petCheckUpInfoList);
        return petHealth;
    }
}
